package cn.scu.ne04jdemo.controller;


import cn.scu.ne04jdemo.entitiy.OwnershipRightChangeEntity;
import cn.scu.ne04jdemo.entitiy.RightApplyEntity;
import cn.scu.ne04jdemo.service.OwnershipRightChangeService;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("/ownership/right/change")
public class OwnershipRightChangeController {

    @Autowired
    private OwnershipRightChangeService ownershipRightChangeService;


    @GetMapping("/findPage")
    public IPage<OwnershipRightChangeEntity> findPage(@RequestParam String dataName
            , @RequestParam(required = true) long current, @RequestParam(required = true)  long size){
        QueryWrapper<OwnershipRightChangeEntity> params = new QueryWrapper();
        params.like(StringUtils.isNotBlank(dataName), "data_name", dataName);
        IPage<OwnershipRightChangeEntity> page = new Page<OwnershipRightChangeEntity>(current, size);
        try {
            page = ownershipRightChangeService.page(page, params);
        }catch (Exception e){
            e.printStackTrace();
        }
        return page;
    }



    @PostMapping("/delById")
    public Boolean delById(@RequestParam(required = true) Integer id){
        try {
            ownershipRightChangeService.removeById(id);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @PostMapping("/save")
    @ResponseBody
    public Boolean save(@RequestBody OwnershipRightChangeEntity ownershipRightChangeEntity){
        try {
            if(!ObjectUtils.isNotNull(ownershipRightChangeEntity))
                ownershipRightChangeEntity = new OwnershipRightChangeEntity();
            ownershipRightChangeService.save(ownershipRightChangeEntity);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @GetMapping("/download")
    @ResponseBody
    public void download(@RequestParam String dataName, HttpServletResponse response) {
        QueryWrapper<OwnershipRightChangeEntity> params = new QueryWrapper();
        params.like(StringUtils.isNotBlank(dataName), "data_name", dataName);
        List<OwnershipRightChangeEntity> ownershipRightChangeEntityList =  ownershipRightChangeService.list(params);
        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("所有权变更.xlsx", "UTF-8");
            response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
            EasyExcel.write(response.getOutputStream(), OwnershipRightChangeEntity.class).sheet().doWrite(ownershipRightChangeEntityList);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
